use std::cmp::Ordering::{Equal, Greater, Less};

struct Solution {}

impl Solution {
  pub fn number_of_weak_characters(properties: Vec<Vec<i32>>) -> i32 {
    let mut p = properties;
    let mut ret = 0;
    p.sort_by(|a, b| {
      if a[0] > b[0] {
        Greater
      } else if a[0] == b[0] {
        if a[1] > b[1] {
          Less
        } else if a[1] < b[1] {
          Greater
        } else {
          Equal
        }
      } else {
        Less
      }
    });
    let l = p.len();
    let mut i = l - 2;
    let mut max = p[l-1][1];
    loop {
      if p[i][1] < max {
        ret += 1;
      }
      max = max.max(p[i][1]);
      i -= 1;
      if i == usize::MAX {
        break;
      }
    }
    ret
  }
}